const app = getApp();
const API_URL = app.globalData.API_URL;

Page({
  data: {
    customBar: app.globalData.CustomBar,
    ROOT_URL: app.globalData.ROOT_URL,
    count:1,
    userName:null,
    clicked:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let id = options.id;
    // 获取首页轮播信息
    wx.request({
      url: API_URL + "getProductDetail",
      method: 'POST',
      data: {
        'id': id
      },
      success(res) {
        that.setData({
          product: res.data.data,
        })
      }
    })
  },

  payment(){
    let that = this;
    let openid = wx.getStorageSync('openid');
    if(that.data.userName){
      this.setData({
        clicked:true,
      });
      let cart = [{'proid':that.data.product.id,'count':that.data.count}];
      wx.request({
        url: API_URL + "submitOrder",
        method: 'POST',
        data: {
          'openid':openid,
          'cart': cart,
          'username': that.data.userName,
          'tel':that.data.telNum,
          'adds':that.data.userAds,
        },
        success(res) {
          let data = res.data;
          data['success'] = ()=>{
            console.log('微信支付成功');
            wx.navigateBack({
              delta: 1,
            })
          };
          data['fail'] = ()=>{
            console.log('微信支付失败');
          };
          wx.requestPayment(data);
        }
      })
    }else{
      console.log("请选择地址");
    }
  },

  addCount(){
    let count = this.data.count;
    count++;
    this.setData({
      count:count,
    })
  },

  subCount(){
    let count = this.data.count;
    if(count>1){
      count--;
    }
    this.setData({
      count:count,
    })
  },

  selectAds(){
    let that = this;
    wx.chooseAddress({
      success: (result) => {
        console.log(result);
        that.setData({
          userName:result.userName,
          telNum:result.telNumber,
          userAds:result.provinceName + result.cityName + result.countyName + result.detailInfo,
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})